import encryption from '@/libs/encryption.js'
import { queryVmWithParty } from '@/api/drill_compete.js'
import { getDictInfoByName } from '@/api/dict.js'
import {hxRouterVms} from '@/api/drill'
const disRouteFun = {
    data(){
		let September = sessionStorage.getItem('November')
        September = JSON.parse(encryption.SelfUseDecrypt(September))
	  	return {
			hxRouterVmList:[],
			hideSingle:'',
			hideAll:'',
			showSingle:'',
			keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
	  	}	
	},
	filters:{

	},
	beforeDestroy(){
		
	},
	watch:{
		
	},
	mounted(){
		this.hideAll = "#!/bin/vbash\nsource /opt/vyatta/etc/functions/script-template\nconfigure\nlancidr=192.168.18\nlaneth=`ip a | grep  $lancidr | awk '{print $7}'`\nfirewall=firewall01\nset firewall name $firewall default-action drop\nset interfaces ethernet $laneth firewall out name $firewall\ncommit\nsave\n";
	    this.showSingle = `#!/bin/vbash
			source /opt/vyatta/etc/functions/script-template
			configure
			firewall=firewall01
			set firewall name $firewall rule 100 action accept
			set firewall name $firewall rule 100 destination address 192.168.18.20/32
			commit
			save`
	    ;
	    this.hideSingle = `
	        #!/bin/vbash
			source /opt/vyatta/etc/functions/script-template
			configure
			firewall=firewall01
			del firewall name $firewall rule 100
			commit
			save
	    `;
	    this.ctfConfig = `#ctf基础配置
			#!/bin/vbash
			source /opt/vyatta/etc/functions/script-template
			configure
			lancidr=192.168.18.1/24`+
			"\nlaneth=`ip a | grep  $lancidr | awk '{print $7}'`\n"+
			"lanip=`ip a | grep  $lancidr | awk '{print $2}'`\n"+
			"lan2cidr=192.168.20.\n"+
			"lan2eth=`ip a | grep  $lan2cidr | awk '{print $7}'`\n"+
			"lan2ip=`ip a | grep  $lan2cidr | awk '{print $2}'`\n"+
			"managercidr=192.168.100.\n"+
			"managereth=`ip a | grep  $managercidr | awk '{print $7}'`\n"+
			"managerip=`ip a | grep  $managercidr | awk '{print $2}'`\n"+
			`#配置端口ip
			del interface ethernet $laneth address dhcp
			set interface ethernet $laneth address $lanip
			del interface ethernet $lan2eth address dhcp
			set interface ethernet $lan2eth address $lan2ip
			del interface ethernet $managereth address dhcp
			set interface ethernet $managereth address $managerip
			#配置本地规则，禁止所有端口
			firename=local2
			set firewall name $firename default-action 'drop' 
			set interfaces ethernet $laneth firewall local name $firename
			firename=local3
			set firewall name $firename default-action 'drop' 
			set interfaces ethernet $lan2eth firewall local name $firename
			commit
			save
			sudo reboot`;
		this.ctfConfig2 = `#!/bin/vbash
			source /opt/vyatta/etc/functions/script-template
			configure
			lancidr=192.168.18`+
			"\nlaneth=`ip a | grep  $lancidr | awk '{print $7}'`\n"+
			`firewall=firewall01
			set firewall name $firewall default-action drop 
			set interfaces ethernet $laneth firewall out name $firewall
			commit
			save`;
		this.adcBaseConfig = `#!/bin/vbash
			source /opt/vyatta/etc/functions/script-template
			configure
			wancidr=192.168.50.
			waneth=\`ip a | grep  $wancidr | awk '{print $7}'\`
			wanip=\`ip a | grep  $wancidr | awk '{print $2}'\`
			lancidr=192.168.18.1/24
			laneth=\`ip a | grep  $lancidr | awk '{print $7}'\`
			lanip=\`ip a | grep  $lancidr | awk '{print $2}'\`
			lan2cidr=192.168.20.
			lan2eth=\`ip a | grep  $lan2cidr | awk '{print $7}'\`
			lan2ip=\`ip a | grep  $lan2cidr | awk '{print $2}'\`
			managercidr=192.168.100.
			managereth=\`ip a | grep  $managercidr | awk '{print $7}'\`
			managerip=\`ip a | grep  $managercidr | awk '{print $2}'\`
			#配置端口ip
			del interface ethernet $waneth address dhcp
			set interface ethernet $waneth address $wanip
			del interface ethernet $laneth address dhcp
			set interface ethernet $laneth address $lanip
			del interface ethernet $lan2eth address dhcp
			set interface ethernet $lan2eth address $lan2ip
			del interface ethernet $managereth address dhcp
			set interface ethernet $managereth address $managerip
			#设置外网网关
			set system gateway-address 192.168.50.1
			#设置nat转发
			set nat source rule 100 outbound-interface $waneth
			set nat source rule 100 source address 0.0.0.0/0
			set nat source rule 100 translation address masquerade
			#配置本地规则，禁止所有端口
			firename=local
			set firewall name $firename default-action 'drop' 
			set interfaces ethernet $waneth firewall local name $firename
			firename=local2
			set firewall name $firename default-action 'drop' 
			set interfaces ethernet $laneth firewall local name $firename
			firename=local3
			set firewall name $firename default-action 'drop' 
			set interfaces ethernet $lan2eth firewall local name $firename
			commit
			save
			sudo reboot`;
        this.getHXAllVmlist();
	},
	methods:{
		getCtfdefaultConfig(){
			return new Promise((resolve,reject) => {
				getDictInfoByName({name:'ctfdefaultConfig'}).then(data => {
					if(data.code == 1){
						this.ctfConfig = data.attribute.data.value;
						resolve(data.attribute.data.value);
					}
				})
			})
		},
		getCtfHideAll(){
			return new Promise((resolve,reject) => {
				getDictInfoByName({name:'allToHidden'}).then(data => {
					if(data.code == 1){
						this.ctfConfig2 = data.attribute.data.value;
						resolve(data.attribute.data.value);
					}
				})
			})
		},
		getDictInfoByName(name){
			return new Promise((resolve,reject) => {
				getDictInfoByName({name}).then(data => {
					if(data.code == 1){
						resolve(data.attribute.data.value);
					}else{
						reject('获取规则失败!');
					}
				}).catch(error => {
					reject('获取规则失败!');
				})
			})
		},
		//获取核心路由列表
	    getHXAllVmlist() {
	        let _this = this;
	        let params = {}
	        params.page = 1;
	        params.pageSize = 2147483647;
	        params.projectId = this.$route.query.prjid;
	        params.findPorts = true;
	        params.sortBy = 'name';
			params.order = 'asc';
	        params.vmType = 'DRT';
	        queryVmWithParty(params.projectId,params).then(data => {
	          _this.hxRouterVmList = [];
	          if (data.code == 1) {
	            _this.hxRouterVmList = data.data.list;
	          }
	        })
	    },
	    //下发核心路由
	    getSelHxRouterVms(code,des) { 
	    	return new Promise((resolve,reject) => {
	    		let str = this.encode(code);
		    	let projectId = this.$route.query.prjid;
		    	let executionIns = this.hxRouterVmList.map(vm => {
	          		return {
		          		content: str,
		                deviceId: vm.id,
		                enableDelete: false,
		                enableWrite: true,
		                ip: "192.168.100.1",
		                password: vm.access[0].password,
		                path: "/home/vyos/rule.sh",
		                port: vm.access[0].port,
		                type: vm.access[0].accessType,
		                username: vm.access[0].username,
		                description:des
		          	}
	            })
		    	hxRouterVms(projectId, executionIns).then(data => {
		            if (data.code == 1) {
		              //this.$Message.info('核心路由器下发成功');
		              resolve(data);
		            } else {
		              this.$Message.error(data.message);
		              reject(data);
		            }
		        }).catch(error => {
		        	reject('下发失败');
		        })
	    	})
      },
        // private method for UTF-8 encoding
      _utf8_encode(string) {
		if (!string) {
			return ''
		}
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

          var c = string.charCodeAt(n);

          if (c < 128) {
            utftext += String.fromCharCode(c);
          } else if ((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
          } else {
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
          }

        }

        return utftext;
      },
      // public method for encoding
      encode(input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;
        input = this._utf8_encode(input);

        while (i < input.length) {

          chr1 = input.charCodeAt(i++);
          chr2 = input.charCodeAt(i++);
          chr3 = input.charCodeAt(i++);

          enc1 = chr1 >> 2;
          enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
          enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
          enc4 = chr3 & 63;

          if (isNaN(chr2)) {
            enc3 = enc4 = 64;
          } else if (isNaN(chr3)) {
            enc4 = 64;
          }

          output = output + this.keyStr.charAt(enc1) + this.keyStr.charAt(enc2) + this.keyStr.charAt(enc3) + this.keyStr
            .charAt(enc4);

        }

        return output;
      },
	}
}
export default disRouteFun
